package cn.edu.bjut.explosion;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import cn.edu.bjut.explosion.Http.HttpUtils;
import cn.edu.bjut.explosion.JsonGson.RegisterJson;

public class RegisterActivity extends AppCompatActivity {

    private EditText r_uname;
    private EditText r_pw;
    private EditText r_pw2;

    private String uname;
    private String pw;
    private String pw2;
    private String strResult;
    private String result;

    String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
    String url2 = "http://dewusang.xin:8080/api/register";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        r_uname = findViewById(R.id.r_uname);
        r_pw = findViewById(R.id.r_pw);
        r_pw2 = findViewById(R.id.r_pw2);

        r_uname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().contains(" ")) {
                    //下面这个就是强行去除空格的鬼东西。。。烦躁
                    String[] str = charSequence.toString().split(" ");
                    String str1 = "";
                    for (int x = 0; x < str.length; x++) {
                        str1 += str[x];
                    }
                    r_uname.setText(str1);
                    r_uname.setError("にたま的别输入空格了！");
                    r_uname.setSelection(i);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        r_pw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().contains(" ")) {
                    //下面这个就是强行去除空格的鬼东西。。。烦躁
                    String[] str = charSequence.toString().split(" ");
                    String str1 = "";
                    for (int x = 0; x < str.length; x++) {
                        str1 += str[x];
                    }
                    r_uname.setText(str1);
                    r_uname.setError("にたま的别输入空格了！");
                    r_uname.setSelection(i);
                }
                if (!r_pw.getText().toString().matches(regex)) {

                    r_pw.setError("密码必须为8~16位数字字母混合！");

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        r_pw2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if (!r_pw.getText().toString().equals(r_pw2.getText().toString())) {

                    r_pw2.setError("两次输入的密码不同");

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }
    public void r_ok(View view) throws InterruptedException {

        uname = r_uname.getText().toString();
        pw = r_pw.getText().toString();
        pw2 = r_pw2.getText().toString();
        if(uname.equals("")) {
            Toast.makeText(RegisterActivity.this,"用户名不能为空",Toast.LENGTH_LONG).show();
        } else {
            if(pw.equals("")) {
                Toast.makeText(RegisterActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
            } else if(!pw.matches(regex)){
                Toast.makeText(RegisterActivity.this,"密码必须为8~16位数字字母混合",Toast.LENGTH_LONG).show();
            } else {
                if(pw2.equals("")) {
                    Toast.makeText(RegisterActivity.this,"请输入确认密码",Toast.LENGTH_LONG).show();
                } else {
                    if (!pw.equals(pw2)) {
                        Toast.makeText(RegisterActivity.this,"两次输入的密码不同",Toast.LENGTH_LONG).show();
                    } else {
                        zhuce();
                        if(result.equals("Register success!")) {
                            Toast.makeText(RegisterActivity.this,"注册成功，请重新登陆",Toast.LENGTH_LONG).show();
                            startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                            finish();
                        } else {
                            Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }
        }

    }
    public void r_back(View view){
        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
        finish();
    }
    public void zhuce() throws InterruptedException {
        Thread regthread = new Thread() {
            @Override
            public void run() {
                super.run();
                try {

                    //提交
                    RecSmsToPost(uname,pw);
                    Gson gson = new Gson();
                    RegisterJson bean = gson.fromJson(strResult,RegisterJson.class);
                    result = bean.getResult();//从处理好的数据中取出所需的数值
                    Log.d("result1",result);

                } catch (Exception e) {
                }

            }

            private void RecSmsToPost(String username,String password) {

                //参数
                Map<String,String> params = new HashMap<String,String>();
                params.put("username", username);
                params.put("password",password);


                //服务器请求路径
                String strUrlPath = url2;
                strResult= HttpUtils.submitPostData(strUrlPath,params, "utf-8");

                Log.d("oraoraoraora:",strResult);
                //openToast("提交完成");

            }
        };
        regthread.start();
        regthread.join();
        regthread.interrupt();
        regthread = null;
    }
}
